package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.CarInfo;
import com.czy.owner.widget.RoundImageView;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.List;

/**
 * Created by King on 2016/6/7 0007.
 */
public class GarageCarAdapter extends BaseRecyclerAdapter<CarInfo, GarageCarAdapter.GarageCarHolder> {
    private OnItemEditListener onItemEditListener;
    private OnItemSetDefaultListener onItemSetDefaultListener;
    private boolean noteditable;

    public GarageCarAdapter(Context context, List<CarInfo> data) {
        super(context, data, false, false);
    }

    public void setNoteditable(boolean noteditable) {
        this.noteditable = noteditable;
    }

    @Override
    public GarageCarHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_garage_recycler, parent, false);
        return new GarageCarHolder(view);
    }

    public void setOnItemEditListener(OnItemEditListener onItemEditListener) {
        this.onItemEditListener = onItemEditListener;
    }

    public void setOnItemSetDefaultListener(OnItemSetDefaultListener onItemSetDefaultListener) {
        this.onItemSetDefaultListener = onItemSetDefaultListener;
    }

    @Override
    public void onBindViewHolder(final GarageCarHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));
        holder.rbDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemSetDefaultListener != null && holder.rbDefault.isChecked() && !data.get(position).isDefault()) {
                    onItemSetDefaultListener.onSet(data.get(position), position);
                }
            }
        });
        holder.ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemEditListener != null) {
                    onItemEditListener.onEdit(data.get(position));
                }
            }
        });
        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CarInfo carInfo = data.get(position);
                if (onItemDeleteListener != null) {
                    onItemDeleteListener.onItemDelete(position, carInfo, v);
                }
            }
        });

    }

    public class GarageCarHolder extends BaseRecyclerAdapter.ViewHolder {
        private RoundImageView ivAvatar;
        private TextView tvName;
        private TextView tvInfo;
        private TextView tvDegree;
        private ImageView ivEdit, ivDelete;
        private RadioButton rbDefault;

        public GarageCarHolder(View itemView) {
            super(itemView);
            ivAvatar = (RoundImageView) itemView.findViewById(R.id.iv_garage_item_avatar);
            tvName = (TextView) itemView.findViewById(R.id.tv_item_name);
            tvInfo = (TextView) itemView.findViewById(R.id.tv_item_info);
            tvDegree = (TextView) itemView.findViewById(R.id.tv_item_degree);
            rbDefault = (RadioButton) itemView.findViewById(R.id.rb_item_default);
            ivDelete = (ImageView) itemView.findViewById(R.id.iv_item_delete);
            ivEdit = (ImageView) itemView.findViewById(R.id.iv_item_edit);
        }

        public void bindData(CarInfo mCarInfo) {
            if (noteditable) {
                ivDelete.setVisibility(View.GONE);
                ivEdit.setVisibility(View.GONE);
            }
            if (!mCarInfo.getImageSrc().isEmpty()) {
                ivAvatar.setTag(mCarInfo.getImageSrc());
                x.image().bind(ivAvatar, mCarInfo.getImageSrc(), new Callback.CommonCallback<Drawable>() {
                    @Override
                    public void onSuccess(Drawable result) {

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        ivAvatar.setImageResource(R.mipmap.default_icon);
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            } else {
                ivAvatar.setImageResource(R.mipmap.default_icon);
            }

            tvName.setText(mCarInfo.getChName());
            tvInfo.setText(mCarInfo.getCarBrandId() + "");
            tvDegree.setText(String.format(mContext.getResources().getString(R.string.my_garage_item_degree_format), mCarInfo.getInfoProcess() + "%"));
            rbDefault.setChecked(mCarInfo.isDefault());
            rbDefault.setTag(mCarInfo);
        }
    }

    public interface OnItemEditListener {
        void onEdit(CarInfo mCarInfo);
    }

    public interface OnItemSetDefaultListener {
        void onSet(CarInfo mCarInfo, int position);
    }

    public void removeCar(int position) {
        data.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, data.size() - position);
    }
}
